package com.shambles.manage2.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.CostDetailService;
import com.shambles.manage2.service.CostService;
import com.shambles.manage2.tkmapper.entity.*;
import com.shambles.manage2.utils.ObjectUtils;
import com.shambles.manage2.utils.TimeUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 费用及明细操作接口
 */
@RestController
@RequestMapping("cost")
public class CostController {
    @Autowired
    private CostService costService;
    @Autowired
    private CostDetailService costDetailService;

    /**
     * 出场费用填写接口
     */
    @PostMapping("insertCost")
    public RestResponse insertCost(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        Cost cost = new Cost();
        BeanUtils.copyProperties(cost, jsonObject.get("cost"));

        JSONArray costDetails = jsonObject.getJSONArray("costDetails");
        List<CostDetail> detailList = costDetails.stream().map(e -> {
            CostDetail costDetail = new CostDetail();
            try {
                BeanUtils.copyProperties(costDetail, e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            return costDetail;
        }).collect(Collectors.toList());
        this.costService.insertCost(cost, detailList);
        return RestResponse.ok();
    }

    /**
     * 出场费用查询接口
     */
    @PostMapping("queryCost")
    public RestResponse queryCost(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        MessageRequestVM messageRequestVM = new MessageRequestVM();
        BeanUtils.populate(messageRequestVM, jsonObject);
        PageInfo<Cost> pageInfo = this.costService.querCostAll(messageRequestVM);
        return RestResponse.ok(pageInfo);
    }

    /**
     * 出场费用明细查询
     */
    @PostMapping("queryCostDetail")
    public List<CostDetail> queryCostDetail(@RequestBody JSONObject jsonObject) {
        String guid = jsonObject.getString("guid");
        return this.costDetailService.queryCostDetail(guid);
    }

    /**
     * 确认收费
     */
    @PostMapping("confirm")
    public RestResponse confirm(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        Cost cost = new Cost();
        BeanUtils.populate(cost, jsonObject);
        cost.setConfirmTime(System.currentTimeMillis());
        costService.confirm(cost);
        return RestResponse.ok();
    }

    /**
     * 统计一段时间内的检疫通过数量,屠宰数量,屠宰金额,预约屠宰数量,默认当月
     */
    @PostMapping("count")
    public RestResponse count(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.count(jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }

    /**
     * 统计一段时间内的宰杀折线图
     */
    @PostMapping("countNum")
    public RestResponse countNum(@RequestBody JSONObject jsonObject) {

        return RestResponse.ok(costService.countNum("number", jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }

    /**
     * 统计一段时间内的检疫通过折线图
     */
    @PostMapping("countQuarantine")
    public RestResponse countQuarantine(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countNum("quarantine_number", jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }

    /**
     * 统计一段时间内的未检疫通过折线图
     */
    @PostMapping("countUnQuarantine")
    public RestResponse countUnQuarantine(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countNum("number - cost_detail.quarantine_number", jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }

    /**
     * 统计一段时间内的收费折线图
     */
    @PostMapping("countMoney")
    public RestResponse countMoney(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countNum("money", jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }


    /**
     * 商户屠宰数量统计
     */
    @PostMapping("countByUser")
    public RestResponse countByUser(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countByUser(jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }

    /**
     * 商户屠宰数量详细统计
     */
    @PostMapping("countByUserDetail")
    public RestResponse countByUserDetail(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countByUserDetail(jsonObject.getString("userGuid"), jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }
    /**
     * 所有商户屠宰数量详细统计
     */
    @PostMapping("countByUsersDetail")
    public RestResponse countByUsersDetail(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countByUsersDetail( jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }
    /**
     * 检疫合格率
     */
    @PostMapping("countRateByType")
    public RestResponse countRateByType(@RequestBody JSONObject jsonObject) {
        return RestResponse.ok(costService.countRateByType( jsonObject.getLong("sTime"), jsonObject.getLong("eTime")));
    }
}
